home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 June / CHIP Haziran 2001.iso / prog / haziran / 19 / setup.exe / data.z / windrvr_int_thread.h < prev    next >
C/C++ Source or Header  |  2001-04-11  |  8KB  |  319 lines

  1. #ifndef _WINDRVR_INT_THREAD_H_
  2. #define _WINDRVR_INT_THREAD_H_
  3.  
  4. #if defined(WIN32) && !defined(WINCE)   
  5.     #include <process.h>
  6. #endif
  7.  
  8. #include "windrvr.h"
  9.  
  10. #ifdef __cplusplus
  11. extern "C" {
  12. #endif  // __cplusplus 
  13.  
  14. #if defined(__KERNEL__)
  15.  
  16.     typedef void (*INT_HANDLER_FUNC)(PVOID pData);
  17.     typedef struct 
  18.     {
  19.         HANDLE hWD;
  20.         WD_INTERRUPT *pInt;
  21.         INT_HANDLER_FUNC func;
  22.         PVOID pData;
  23.     } INT_THREAD_DATA;
  24.  
  25.     static void __cdecl InterruptThreadHandler (PVOID pContext)
  26.     {
  27.         INT_THREAD_DATA *pThread = (INT_THREAD_DATA *) pContext;
  28.  
  29.         WD_IntCount(pThread->hWD, pThread->pInt);
  30.         pThread->func(pThread->pData);
  31.     }
  32.  
  33.     static BOOL InterruptThreadEnable(HANDLE *phThread, HANDLE hWD, 
  34.         WD_INTERRUPT *pInt, INT_HANDLER_FUNC func, PVOID pData)
  35.     {
  36.         INT_THREAD_DATA *pThread;
  37.         *phThread = NULL;
  38.  
  39.         pThread = (INT_THREAD_DATA *) malloc(sizeof (INT_THREAD_DATA));
  40.         if (!pThread)
  41.             return FALSE;
  42.  
  43.         pInt->kpCall.hKernelPlugIn = WD_KERNEL_DRIVER_PLUGIN_HANDLE;
  44.         pInt->kpCall.dwMessage = (DWORD) InterruptThreadHandler;
  45.         pInt->kpCall.pData = pThread;
  46.         WD_IntEnable (hWD, pInt);
  47.         // check if WD_IntEnable failed
  48.         if (!pInt->fEnableOk)
  49.             return FALSE;
  50.         
  51.         pThread->pInt = pInt;
  52.         pThread->hWD = hWD;
  53.         pThread->func = func;
  54.         pThread->pData = pData;
  55.  
  56.         *phThread = (HANDLE) pThread;
  57.         return TRUE;
  58.     }
  59.  
  60.     static void InterruptThreadDisable(HANDLE hThread)
  61.     {
  62.         if (hThread)
  63.         {
  64.             INT_THREAD_DATA *pThread = (INT_THREAD_DATA *) hThread;
  65.             WD_IntDisable(pThread->hWD, pThread->pInt);
  66.             free (pThread);
  67.         }
  68.     }
  69. #else
  70.     #if !defined(VXWORKS)
  71.         #include <malloc.h>
  72.         #if defined(UNIX)
  73.             #include <pthread.h>
  74.         #endif
  75.     #endif
  76.     
  77.     typedef void (*HANDLER_FUNC)(void *data);
  78.  
  79.     #if defined(VXWORKS)
  80.         #define WAIT_FOR_EVER 0
  81.         static void vxTask_wait(int taskId, int waitTime)
  82.         {
  83.             SEM_ID waitSem;
  84.             
  85.             if(waitTime == WAIT_FOR_EVER)
  86.             {
  87.                 /* Loop while task is still alive */
  88.                 while(taskIdVerify(taskId) == OK)
  89.                     taskDelay(3);
  90.             }
  91.             else
  92.             {
  93.                 /* 
  94.                  * create a dummy semaphore and try to take it for the specified
  95.                  * time.
  96.                  */ 
  97.                 waitSem = semBCreate(SEM_Q_PRIORITY , SEM_EMPTY);
  98.                 semTake(waitSem, waitTime);
  99.                 semDelete(waitSem);
  100.             }
  101.         }
  102.     #endif
  103.  
  104.     typedef struct 
  105.     {
  106.         HANDLER_FUNC func;
  107.         void *data;
  108.     } thread_struct_t;
  109.  
  110.     #if defined(WIN32) && !defined (WINCE)
  111.        static unsigned int WINAPI thread_handler(void *data)
  112.         #elif defined(WINCE)
  113.             static unsigned long WINAPI thread_handler(void *data)
  114.     #elif defined(OS2)
  115.         static void thread_handler(void *data)
  116.         #else
  117.     static void* thread_handler(void *data)
  118.     #endif
  119.     {
  120.         thread_struct_t *t = (thread_struct_t *)data;
  121.         t->func(t->data);
  122.         free(t);
  123.         #if !defined(OS2)
  124.             return 0;
  125.         #endif
  126.     }
  127.     
  128.     static void *thread_start(HANDLER_FUNC func, void *data)
  129.     {
  130.         thread_struct_t *t = (thread_struct_t *) malloc(sizeof(thread_struct_t));
  131.         void *ret = NULL;
  132.         
  133.         t->func = func;
  134.         t->data = data;
  135.         #if defined(WIN32) && !defined(WINCE)
  136.             ret = (void *) _beginthreadex(NULL, 0x1000, 
  137.                 thread_handler, (void *) t, 0, (unsigned int *) &GlobalDW);
  138.         #elif defined(WINCE)
  139.             ret = (void *) CreateThread ( NULL, 0x1000, 
  140.                 thread_handler,(void *) t, 0, (unsigned long *) &GlobalDW);
  141.         #elif defined(OS2)
  142.             ret = (void *) _beginthread(thread_handler, NULL, 0x4000, (void *)t);
  143.         #elif defined(VXWORKS)
  144.             {
  145.                 int priority;
  146.                 if (taskPriorityGet(taskIdSelf(), &priority)!=ERROR)
  147.                     ret = (PVOID) taskSpawn(NULL, priority, 0, 2000, 
  148.                         (FUNCPTR)thread_handler, (int)t, 0, 0, 0, 0, 0,
  149.                         0, 0, 0, 0);
  150.                 if ((int) ret == ERROR)
  151.                     ret = NULL;
  152.             }
  153.         #elif defined(UNIX)
  154.             {
  155.                 int err;
  156.                 ret = malloc(sizeof(pthread_t));
  157.                 err = pthread_create((pthread_t *)ret, NULL, thread_handler, (PVOID) t);
  158.                 if (err)
  159.                 {
  160.                     free(ret);
  161.                     ret = NULL;
  162.                 }
  163.             }
  164.         #endif
  165.         if (!ret)
  166.             free(t);
  167.         return ret;
  168.     }
  169.  
  170.     static void thread_stop(void *thread)
  171.     {
  172.         #if defined(WIN32)
  173.             WaitForSingleObject((HANDLE) thread, INFINITE);
  174.             CloseHandle(thread);
  175.         #elif defined(OS2)
  176.             DosWaitThread((ULONG *)&thread, DCWW_WAIT);
  177.         #elif defined(VXWORKS)
  178.             vxTask_wait((int) thread, WAIT_FOR_EVER);
  179.         #elif defined(UNIX)
  180.             pthread_join((pthread_t) thread, NULL);
  181.             free(thread);
  182.         #endif
  183.     }
  184.     
  185.     typedef struct 
  186.     {
  187.         HANDLE hWD;
  188.         HANDLER_FUNC func;
  189.         PVOID pData;    
  190.         WD_INTERRUPT *pInt;     
  191.         void *thread;
  192.     } INT_THREAD_DATA;
  193.  
  194.       static void interrupt_thread_handler(void *data)
  195.     {
  196.         INT_THREAD_DATA *pThread = (INT_THREAD_DATA *) data;
  197.         for (;;)
  198.         {
  199.             WD_IntWait (pThread->hWD, pThread->pInt);
  200.             if (pThread->pInt->fStopped)
  201.                 break;
  202.             pThread->func(pThread->pData);
  203.         }
  204.     }
  205.     
  206.     static BOOL InterruptThreadEnable(HANDLE *phThread, HANDLE hWD, 
  207.         WD_INTERRUPT *pInt, HANDLER_FUNC func, PVOID pData)
  208.     {
  209.         INT_THREAD_DATA *pThread;
  210.         *phThread = NULL;
  211.  
  212.         WD_IntEnable (hWD, pInt);
  213.         // check if WD_IntEnable failed
  214.         if (!pInt->fEnableOk)
  215.             return FALSE;
  216.         
  217.         pThread = (INT_THREAD_DATA *) malloc(sizeof (INT_THREAD_DATA));
  218.         BZERO(*pThread);
  219.         pThread->func = func;
  220.         pThread->pData = pData;
  221.         pThread->hWD = hWD;
  222.         pThread->pInt = pInt;
  223.  
  224.         pThread->thread = thread_start(interrupt_thread_handler, (void *)pThread);
  225.         if(!pThread->thread)
  226.         {
  227.             free(pThread);
  228.             return FALSE;
  229.         }
  230.         *phThread = (HANDLE) pThread;
  231.         return TRUE;
  232.     }
  233.     
  234.     static VOID InterruptThreadDisable(HANDLE hThread)
  235.     {
  236.         if (hThread)
  237.         {
  238.             INT_THREAD_DATA *pThread = (INT_THREAD_DATA *) hThread;
  239.             WD_IntDisable(pThread->hWD, pThread->pInt);
  240.             thread_stop(pThread->thread);
  241.             free (pThread);
  242.         }
  243.     }
  244.  
  245.     typedef void (*cpci_event_func)(WD_CPCI_EVENT *event, void *data);
  246.  
  247.     typedef struct
  248.     {
  249.         cpci_event_func func;
  250.         HANDLE hWD;
  251.         void *data;
  252.         HANDLE thread; 
  253.         WD_INTERRUPT Int;
  254.     } hs_event_handle_t; 
  255.  
  256.     static void hs_event_handler(void *h)
  257.     {
  258.         hs_event_handle_t *handle = (hs_event_handle_t *)h;
  259.         WD_HS_REGISTER_EVENT pull;
  260.  
  261.         BZERO(pull);
  262.         pull.hInterrupt = handle->Int.hInterrupt;
  263.         WD_HsEventPull(handle->hWD, &pull);
  264.         if (pull.cpciEvent.dwAction)
  265.             handle->func(&pull.cpciEvent, handle->data);
  266.     }
  267.  
  268.     static hs_event_handle_t *hs_register_event(HANDLE hWD, WD_CPCI_EVENT *event, cpci_event_func func, void *data)
  269.     {
  270.         hs_event_handle_t *handle = NULL;
  271.         WD_HS_REGISTER_EVENT event_register;
  272.  
  273.         handle = (hs_event_handle_t *)malloc(sizeof(*handle));
  274.         if (!handle)
  275.             goto Error;
  276.         BZERO(*handle);
  277.         handle->func = func;
  278.         handle->hWD = hWD; 
  279.         handle->data = data;
  280.  
  281.         BZERO(event_register);
  282.         event_register.cpciEvent = *event;
  283.         WD_HsEventRegister(hWD, &event_register);
  284.         if (!event_register.hInterrupt)
  285.             goto Error;
  286.         handle->Int.hInterrupt = event_register.hInterrupt; 
  287.         if (!InterruptThreadEnable(&handle->thread, hWD, &handle->Int, hs_event_handler, (PVOID) handle))
  288.             goto Error;
  289.         if (!handle->Int.fEnableOk)
  290.             goto Error;
  291.         return handle;
  292.  
  293.     Error:
  294.         if (handle && handle->Int.hInterrupt)
  295.             WD_HsEventUnregister(hWD, handle);
  296.         if (handle)
  297.             free(handle);
  298.         return NULL;
  299.     }
  300.  
  301.     static void hs_unregister_event(HANDLE hWD, hs_event_handle_t *handle)
  302.     {
  303.         WD_HS_REGISTER_EVENT event;
  304.         BZERO(event);
  305.         event.hInterrupt = handle->Int.hInterrupt;
  306.         InterruptThreadDisable(handle->thread);
  307.         WD_HsEventUnregister(hWD, &event);
  308.         free(handle);
  309.     }
  310.  
  311. #endif
  312.  
  313. #ifdef __cplusplus
  314. }
  315. #endif  // __cplusplus 
  316.  
  317. #endif
  318.  
  319.